//
//  ImageTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/8/4.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ImageTableViewCell.h"
#import "PhotoMode.h"
#import <UIImageView+WebCache.h>
#import "SDPhotoBrowser.h"
@interface ImageTableViewCell ()<UIGestureRecognizerDelegate,SDPhotoBrowserDelegate>
@property (nonatomic,strong) UUImageAvatarBrowser *brower;
@end

@implementation ImageTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:
    reuseIdentifier]) {
        [self setupLayout];
    }
    return self;
}
- (void)awakeFromNib {
    // Initialization code
}
+(instancetype)cellWithTableview:(UITableView *)tableview{
    ImageTableViewCell *cell=[tableview dequeueReusableCellWithIdentifier:@"imagecell"];
    if (cell==nil) {
        cell=[[ImageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"imagecell"];
    }
    return cell;
}



-(void)setupLayout{
    UILabel *lable=[[UILabel alloc]init];
    lable.font=[UIFont systemFontOfSize:15];
    lable.textColor=UIColorWithRGB(164, 164, 164);
    [self.contentView addSubview:lable];
    self.titlelable=lable;
    self.titlelable.font=[UIFont systemFontOfSize:15];
    self.titlelable.textColor=UIColorWithRGB(149, 149, 149);
    self.titlelable.text=@"图片";
    UIView *photoview=[[UIView alloc] init];
    [self.contentView addSubview:photoview];
    self.photoview=photoview;
    CGFloat photoviewwith=XHScreenwidth-64-8;
    CGFloat photoviewx=56;
    CGFloat photoviewy=16;
    
    
    CGFloat photoviewheight;
    photoviewheight=((photoviewwith-24)/4)*2+8;
    CGFloat imgwidth=((photoviewwith-24)/4);
    CGFloat imgheight=imgwidth;
    CGFloat imgx;
    CGFloat imgy;
    
    self.photoview.frame=CGRectMake(photoviewx, photoviewy, photoviewwith, photoviewheight);
    for (int i=0; i<8; i++) {
        
        UIImageView *imageview=[[UIImageView alloc]init];
        if (i==0) {
            imgx=0;
            imgy=0;
        }else if(i>0&&i<4){
            imgx=i*(imgwidth+8);
            imgy=0;
        }else if(i==4){
            imgx=0;
            imgy=imgheight+8;
        }else{
            imgx=(i-4)*(imgwidth+8);
            imgy=imgheight+8;
            
        }
        imageview.frame=CGRectMake(imgx, imgy, imgwidth, imgheight);
        imageview.clipsToBounds=YES;
        imageview.contentMode=UIViewContentModeScaleAspectFill;
        imageview.userInteractionEnabled=YES;
        imageview.tag=i;
        imageview.hidden=YES;
        [self.photoview addSubview:imageview];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
        [imageview addGestureRecognizer:tap];
        
    }

}
-(void)setMode:(PhotoMode *)mode{
    _mode=mode;

    self.titlelable.text=_mode.title;
     CGFloat photoviewwith=XHScreenwidth-64-8;
    CGFloat photoviewheight;
    CGFloat photoviewy=16;
    if (_mode.imgurls.count>4) {
        photoviewheight=((photoviewwith-24)/4)*2+8;
    }else{
        photoviewheight=((photoviewwith-24)/4);
    }
  
    
    NSMutableArray *marray=[NSMutableArray array];
    for (int i=0; i<_mode.imgurls.count; i++) {
        if(i>=8) return;
        UIImageView *imageview=self.photoview.subviews[i];
        imageview.hidden=NO;
             NSString *pic=[NSString getImageURLstr:_mode.imgurls[i]];
        [imageview sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placehoder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error==nil) {
                [marray addObject:image];
            }
        }];
       
    }
     _mode.imagearray=marray;
    CGFloat labelx=16;
    CGFloat lableheight=20;
    CGFloat labley=((photoviewheight+photoviewy+16)-lableheight)/2;
    CGFloat lablewidth=32;
    self.titlelable.frame=CGRectMake(labelx, labley, lablewidth, lableheight);
   
}

-(void)imgclick:(UITapGestureRecognizer *)tap{
    NSLog(@"click");
//    NSLog(@"btntag%d",btn.tag);
    
    UIImageView *imageview=tap.view;
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
  
    browser.sourceImagesContainerView = self.photoview; // 原图的父控件
    browser.imageCount = _mode.imgurls.count; // 图片总数
    browser.currentImageIndex = imageview.tag;
    browser.delegate = self;
    [browser show];
    
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    UIImageView *image=self.photoview.subviews[index];
    
    return image.image;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
